package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.risetechnology.adapter.CustomExpandableListAdapter;
import com.risetechnology.adapter.MethodListAdapter;
import com.risetechnology.common.Const;
import com.risetechnology.common.Shared;
import com.risetechnology.customviews.CustomFontTextView;
import com.risetechnology.data.Ingredient;
import com.risetechnology.data.Method;
import com.risetechnology.data.Recipe;
import com.risetechnology.master.NavigationActivity;

public class ActivityMasalasRecipeDetails extends NavigationActivity {

	private TextView textviewIngredients;
	private LinearLayout frameIngredients;

	private TextView textviewMethod;
	private LinearLayout frameMethod;

	private LinearLayout layoutAdd;

	private TextView textviewRecipeName;
	private TextView textviewServes;

	private TextView textviewMealType;
	private TextView textviewDescription;

	private ImageView imageviewImage;

	private TextView textviewPart1Name;
	private LinearLayout layoutPart1Ingredients;

	private TextView textviewPart2Name;
	private LinearLayout layoutPart2Ingredients;

	private TextView textviewPart3Name;
	private LinearLayout layoutPart3Ingredients;

	/*
	 * Method tab
	 */
	private TextView mTvServingTitle;
	private TextView mTvServingOption;

	private TextView textviewRecipeMethodName;

	private ExpandableListView listviewMethod;
	private List<String> listDataHeader;
	private CustomExpandableListAdapter methodsListAdapter;
	private HashMap<String, List<Method>> listDataChild;

	private Recipe recipe;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_masalas_recipe_details);

		// Set contents
		setContents();
	}

	private void setContents() {
		int recipeId = getIntent().getIntExtra(Const.KEY_ID, 0);
		recipe = Shared.dbAdapter.getRecipeById(recipeId);

		findViewById(R.id.layout_rate).setVisibility(View.GONE);
		findViewById(R.id.layout_fav).setVisibility(View.GONE);

		frameIngredients = (LinearLayout) findViewById(R.id.frame_ingredients);
		frameMethod = (LinearLayout) findViewById(R.id.frame_method);

		textviewIngredients = (CustomFontTextView) findViewById(R.id.tv_ingredients);
		textviewIngredients.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				textviewIngredients.setBackgroundColor(getResources().getColor(
						R.color.bg_tab_selected));
				textviewIngredients.setTextColor(getResources().getColor(
						R.color.white));

				textviewMethod.setBackgroundColor(getResources().getColor(
						R.color.bg_tab_normal));
				textviewMethod.setTextColor(getResources().getColor(
						R.color.gray));

				frameIngredients.setVisibility(View.VISIBLE);
				frameMethod.setVisibility(View.GONE);
			}
		});

		textviewMethod = (CustomFontTextView) findViewById(R.id.tv_method);
		textviewMethod.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				textviewMethod.setBackgroundColor(getResources().getColor(
						R.color.bg_tab_selected));
				textviewMethod.setTextColor(getResources().getColor(
						R.color.white));

				textviewIngredients.setBackgroundColor(getResources().getColor(
						R.color.bg_tab_normal));
				textviewIngredients.setTextColor(getResources().getColor(
						R.color.gray));

				frameIngredients.setVisibility(View.GONE);
				frameMethod.setVisibility(View.VISIBLE);
			}
		});

		layoutAdd = (LinearLayout) frameIngredients
				.findViewById(R.id.layout_add);
		layoutAdd.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(ActivityMasalasRecipeDetails.this
						.getParent(), ActivityAddIngredients.class);
				intent.putExtra(Const.KEY_ID, recipe.getId());
				goNextHistory("Add Ingredients", intent);
			}
		});

		textviewRecipeName = (TextView) frameIngredients
				.findViewById(R.id.tv_name);
		String name = recipe.getName();
		textviewRecipeName.setText(name);

		textviewServes = (TextView) frameIngredients
				.findViewById(R.id.tv_serves);
		textviewServes.setText(recipe.getServes());

		textviewMealType = (TextView) frameIngredients
				.findViewById(R.id.tv_meal_type);
		String[] mealTypes = getResources().getStringArray(R.array.meal_types);
		textviewMealType.setText(mealTypes[recipe.getMealType()]);

		textviewDescription = (TextView) frameIngredients
				.findViewById(R.id.tv_description_contents);
		String description = recipe.getMainDescription();
		textviewDescription.setText(description);

		imageviewImage = (ImageView) frameIngredients
				.findViewById(R.id.iv_image);
		imageviewImage.setImageDrawable(recipe.getImage());

		/*
		 * Method tab
		 */
		if (recipe.getServingOptions().trim().length() > 0) {
			mTvServingTitle = (TextView) frameMethod
					.findViewById(R.id.tv_serving_title);
			mTvServingTitle.setText("Serving Options");

			mTvServingOption = (TextView) frameMethod
					.findViewById(R.id.tv_serving_option);
			mTvServingOption.setText(recipe.getServingOptions());
		}

		
		textviewRecipeMethodName = (TextView) frameMethod
				.findViewById(R.id.tv_name);
		textviewRecipeMethodName.setText(name);

		listviewMethod = (ExpandableListView) frameMethod
				.findViewById(R.id.lv_recipe_method);
		listviewMethod.setGroupIndicator(null);
		listviewMethod
				.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
					@Override
					public boolean onGroupClick(ExpandableListView parent,
							View v, int groupPosition, long id) {
						// Doing nothing
						return true;
					}
				});

		prepareListData();

		listviewMethod.setAdapter(methodsListAdapter);
		methodsListAdapter = new CustomExpandableListAdapter(this,
				listDataHeader, listDataChild);

		// setting list adapter
		listviewMethod.setAdapter(methodsListAdapter);

		// Expand list
		int count = methodsListAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			listviewMethod.expandGroup(i);
		}

		/**
		 * Ingredient
		 */
		textviewPart1Name = (TextView) frameIngredients
				.findViewById(R.id.tv_part_1_name);
		if (recipe.getPart1Name() == null
				|| recipe.getPart1Name().length() == 0
				|| recipe.getPart1Name().equalsIgnoreCase("Part 1 Name...")) {
			textviewPart1Name.setVisibility(View.GONE);
		} else {
			textviewPart1Name.setText(recipe.getPart1Name());
		}
		layoutPart1Ingredients = (LinearLayout) frameIngredients
				.findViewById(R.id.layout_part_1_ingredients);

		for (Ingredient ing : Shared.dbAdapter.getIngByRecipe(recipe.getId(),
				Ingredient.INGRE1)) {
			CustomFontTextView ingredientView = new CustomFontTextView(
					ActivityMasalasRecipeDetails.this.getParent());
			ingredientView.setText(ing.toString());
			ingredientView.setTextColor(getResources().getColor(R.color.black));
			layoutPart1Ingredients.addView(ingredientView);
		}

		textviewPart2Name = (TextView) frameIngredients
				.findViewById(R.id.tv_part_2_name);
		if (recipe.getPart2Name() == null
				|| recipe.getPart2Name().length() == 0
				|| recipe.getPart2Name().equalsIgnoreCase("Part 2 Name...")) {
			textviewPart2Name.setVisibility(View.GONE);
		} else {
			textviewPart2Name.setText(recipe.getPart2Name());
		}
		layoutPart2Ingredients = (LinearLayout) frameIngredients
				.findViewById(R.id.layout_part_2_ingredients);
		for (Ingredient ing : Shared.dbAdapter.getIngByRecipe(recipe.getId(),
				Ingredient.INGRE2)) {
			CustomFontTextView ingredientView = new CustomFontTextView(
					ActivityMasalasRecipeDetails.this.getParent());
			ingredientView.setText(ing.toString());
			ingredientView.setTextColor(getResources().getColor(R.color.black));
			layoutPart2Ingredients.addView(ingredientView);
		}

		textviewPart3Name = (TextView) frameIngredients
				.findViewById(R.id.tv_part_3_name);
		if (recipe.getPart3Name() == null
				|| recipe.getPart3Name().length() == 0
				|| recipe.getPart3Name().equalsIgnoreCase("Part 3 Name...")) {
			textviewPart3Name.setVisibility(View.GONE);
		} else {
			textviewPart3Name.setText(recipe.getPart3Name());
		}
		layoutPart3Ingredients = (LinearLayout) frameIngredients
				.findViewById(R.id.layout_part_3_ingredients);
		for (Ingredient ing : Shared.dbAdapter.getIngByRecipe(recipe.getId(),
				Ingredient.INGRE3)) {
			CustomFontTextView ingredientView = new CustomFontTextView(
					ActivityMasalasRecipeDetails.this.getParent());
			ingredientView.setText(ing.toString());
			ingredientView.setTextColor(getResources().getColor(R.color.black));
			layoutPart3Ingredients.addView(ingredientView);
		}
	}

	private void prepareListData() {
		listDataHeader = new ArrayList<String>();
		listDataChild = new HashMap<String, List<Method>>();

		String method1Name = recipe.getPart1MethodName();
		String method1 = recipe.getPart1Method();

		String method2Name = recipe.getPart2MethodName();
		String method2 = recipe.getPart2Method();

		String method3Name = recipe.getPart3MethodName();
		String method3 = recipe.getPart3Method();

		// Part 1
		List<Method> methodPart1 = new ArrayList<Method>();
		if (method1 != null && method1.length() > 0) {
			String[] splittedMethod = method1.split("#");
			if (splittedMethod != null && splittedMethod.length > 0) {
				int length = splittedMethod.length;
				for (int i = 0; i < length; i++) {
					String mt = splittedMethod[i];
					if (mt.length() > 0)
						methodPart1.add(new Method(mt));
				}
			}
		}

		// Part 2
		List<Method> methodPart2 = new ArrayList<Method>();
		if (method2 != null && method2.length() > 0) {
			String[] splittedMethod = method2.split("#");
			if (splittedMethod != null && splittedMethod.length > 0) {
				int length = splittedMethod.length;
				for (int i = 0; i < length; i++) {
					String mt = splittedMethod[i];
					if (mt.length() > 0)
						methodPart2.add(new Method(mt));
				}
			}
		}

		// Part 3
		List<Method> methodPart3 = new ArrayList<Method>();
		if (method3 != null && method3.length() > 0) {
			String[] splittedMethod = method3.split("#");
			if (splittedMethod != null && splittedMethod.length > 0) {
				int length = splittedMethod.length;
				for (int i = 0; i < length; i++) {
					String mt = splittedMethod[i];
					if (mt.length() > 0)
						methodPart3.add(new Method(mt));
				}
			}
		}

		// if (!method1Name.equals("Part 1 Name...")) {
		listDataHeader.add(method1Name);
		listDataChild.put(method1Name, methodPart1);
		// }
		// if (!method2Name.equals("Part 2 Name...")) {
		listDataHeader.add(method2Name);
		listDataChild.put(method2Name, methodPart2);
		// }
		// if (!method3Name.equals("Part 3 Name...")) {
		listDataHeader.add(method3Name);
		listDataChild.put(method3Name, methodPart3);
		// }
	}
}
